package com.xiaoxin.experience.api.tree.model;

import com.xiaoxin.experience.service.tree.common.AnyTree;
import com.xiaoxin.experience.service.tree.common.BranchProxy;
import com.xiaoxin.experience.service.tree.common.LeafProxy;

import java.util.List;


/**
 * @author xiaoxin
 */
public class InstanceTree extends AnyTree<DirectoryInfo, InstanceInfo,Integer>
{
    public InstanceTree(List<DirectoryInfo> list)
    {
        super(new InstanceTree.DirectoryProxy(), new InstanceTree.InstanceProxy());
        createTree(list);
    }

    public InstanceTree(List<DirectoryInfo> list, DirectoryInfo backRoot, boolean forceRoot)
    {
        super(new InstanceTree.DirectoryProxy(), new InstanceTree.InstanceProxy());
        createTree(list, backRoot, forceRoot);
    }


    private static class DirectoryProxy implements BranchProxy<DirectoryInfo,Integer>
    {

        @Override
        public Integer branchMark(DirectoryInfo object)
        {
            return object.getId();
        }

        @Override
        public Integer branchFrom(DirectoryInfo object)
        {
            return object.getParentId();
        }

        @Override
        public void branchReset(DirectoryInfo object, Integer parent)
        {
            object.setParentId(parent == null ? -1 : parent);
        }
    }

    private static class InstanceProxy implements LeafProxy<InstanceInfo,Integer>
    {
        @Override
        public Integer branchMark(InstanceInfo object)
        {
            return object.getDirectoryId();
        }
    }
}
